package com.bjsxt.userscenter.common.util;

import javax.annotation.Resource;
import javax.mail.internet.MimeMessage;

import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
@Component
public class SpringEmailUtil
{
	@Resource(name="javaMailSenderImpl")
	private JavaMailSender javaMailSender;
	@Resource
	private SimpleMailMessage simpleMailMessage;
	/**
	 * 发送邮件
	 * @param to
	 * @param subject
	 * @param text
	 * @return
	 */
	public boolean sendMail(String to,String subject,String text)
	{
		try
		{
			this.simpleMailMessage.setTo(to);
			this.simpleMailMessage.setSubject(subject);
			this.simpleMailMessage.setText(text);
			this.javaMailSender.send(this.simpleMailMessage);
			return true;
		} catch (MailException e)
		{
			// TODO Auto-generated catch block
			ConstatFinalUtil.LOGGER.error("---- 发送邮件失败了 ---to:{},subject:{},text:{}-",
					to,subject,text,e);

		}
		return false;
	}
	/**
	 * 发送邮件解析html
	 * @param to
	 * @param subject
	 * @param text
	 * @return
	 */
	public boolean sendMailHtml(String to,String subject,String text)
	{
		try
		{
			MimeMessage message=this.javaMailSender.createMimeMessage();
			MimeMessageHelper helper=new MimeMessageHelper(message,true,"utf-8");
			helper.setFrom(this.simpleMailMessage.getFrom());
			helper.setTo(to);
			helper.setSubject(subject);
			helper.setText(text,true);
			this.javaMailSender.send(message);
			ConstatFinalUtil.LOGGER.info("---- 发送邮件成功了 ---to:{},subject:{},text:{}-",
					to,subject,text);
			return true;
		} catch (Exception e)
		{
			// TODO Auto-generated catch block
			ConstatFinalUtil.LOGGER.error("---- 发送邮件失败了 ---to:{},subject:{},text:{}-",
					to,subject,text,e);

		}
		return false;
	}

}
